//
//  WHYEmoticonPackage.swift
//  emoticonKeyboard
//
//  Created by 王洪运 on 15/11/29.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

class WHYEmoticonPackage: NSObject {

     /// 表情包路径
    var id: String?
     /// 表情包名称
    var group_name_cn: String?
     /// 表情包数组
    lazy var emoticons = [WHYEmoticon]()
    
    
    init(dict: [String: AnyObject]) {
        super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        var index = 0
        
        if let array = dict["emoticons"] as? [[String: AnyObject]] {
            for var d in array {
                if let png = d["png"] as? String, let dir = id {
                    d["png"] = dir + "/" + png
                }
                
                emoticons.append(WHYEmoticon(dict: d))
                
                if ++index == 20 {
                    emoticons.append(WHYEmoticon(isRemove: true))
                    index = 0
                }
            }
        }
        
        appendEmptyButton()
    }
    
    private func appendEmptyButton () {
        let count = emoticons.count % 21
        
        if emoticons.count > 0 && count == 0 {
            return
        }
        
        for _ in count..<20 {
            emoticons.append(WHYEmoticon(isEmpty: true))
        }
        
        emoticons.append(WHYEmoticon(isRemove: true))
    }
    
    override var description: String {
        let keys = ["id", "group_name_cn", "emotions"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
}
